/* 
    JSPWiki - a JSP-based WikiWiki clone.

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.  
 */
package com.ecyrd.jspwiki.dav;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.WikiEngine;
import com.ecyrd.jspwiki.WikiPage;
import com.ecyrd.jspwiki.dav.items.DavItem;
import com.ecyrd.jspwiki.dav.items.DirectoryItem;
import com.ecyrd.jspwiki.dav.items.PageDavItem;
import com.ecyrd.jspwiki.providers.ProviderException;
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheFactory;
import net.sf.jsr107cache.CacheManager;

/**
 *  Implements something for the pages.
 *  
 *
 *  @since
 */
public class RawPagesDavProvider extends WikiDavProvider
{
    protected static final Logger log = Logger.getLogger( RawPagesDavProvider.class.getName() );
    
    private Cache cache;

    private Map<String, DavItem> m_davItemCache = new HashMap<String, DavItem>();
    
    private int m_refreshPeriod = 30*1000; // In millisseconds
    
    
    public RawPagesDavProvider( WikiEngine engine )
    {
        super(engine);
        if (cache == null) {
        	CacheFactory cacheFactory = null;
    		try {
    			cacheFactory = CacheManager.getInstance().getCacheFactory();
    			Map props = new HashMap();
    	        props.put(GCacheFactory.EXPIRATION_DELTA, 3600);
    	        cache = cacheFactory.createCache(props);
    		} catch (CacheException e) {
    			log.log(Level.SEVERE, "Exception in getting the Cache", e);
    		}
        }
    }

    protected Collection listAlphabeticals( DavPath path )
    {
        ArrayList<Character> charList = new ArrayList<Character>();
        
        try
        {
            Collection allPages = m_engine.getPageManager().getAllPages();
            
            for( Iterator i = allPages.iterator(); i.hasNext(); )
            {
                String pageName = ((WikiPage)i.next()).getName();
                
                Character firstChar = Character.valueOf(Character.toLowerCase(pageName.charAt(0)));
                
                if( !charList.contains( firstChar ) )
                {
                    charList.add( firstChar );
                }
            }
        }
        catch( ProviderException e )
        {
            log.log(Level.SEVERE,"Could not fetch a list of all pages:",e);
        }
        
        Collections.sort( charList );
        
        ArrayList<DavItem> result = new ArrayList<DavItem>();
        
        for( Iterator i = charList.iterator(); i.hasNext(); )
        {
            Character c = (Character)i.next();
            
            result.add( new DirectoryItem(this, new DavPath(c.toString())) );
        }
        
        return result;
    }

    // FIXME: This is wasteful; this should really keep a structure of its
    // own in memory
    
    private Collection listDirContents( DavPath path )
    {
        String st = path.getName();
        
        log.log(Level.INFO,"Listing contents for dir "+st);
        ArrayList<DavItem> davItems = new ArrayList<DavItem>();
        
        try
        {
            Collection allPages = m_engine.getPageManager().getAllPages();
        
            for( Iterator i = allPages.iterator(); i.hasNext(); )
            {
                WikiPage p = (WikiPage)i.next();
                
                if( p.getName().toLowerCase().startsWith(st) )
                {
                    DavPath np = new DavPath( path );
                    np.append( p.getName()+".txt" );
                    
                    DavItem di = new PageDavItem( this, np, p );
                
                    davItems.add( di );
                }
            }
        }
        catch( ProviderException e )
        {
            log.log(Level.SEVERE,"Unable to fetch a list of all pages",e);
            // FIXME
        }
        return davItems;
    }

    public Collection listItems( DavPath path )
    {
        log.log(Level.INFO,"Listing dav path "+path+", size="+path.size());
        
        switch( path.size() )
        {
            case 1:
                return listAlphabeticals(path);
                
            case 2:
                return listDirContents( path );
                
            default:
                return null;
        }
    }

    protected String getRelativePath( String path )
    {
        if( path.length() > 0 )
        {
            char c = Character.toLowerCase( path.charAt(0) );
        
            return Character.toString(c);
        }
        
        return "";
    }
    
    public String getURL( DavPath path )
    {
        return m_engine.getURL( WikiContext.NONE, DavUtil.combineURL("dav/raw/",path.getPath()),
                                null, true );
    }
    
    public DavItem getItem( DavPath dp )
    {
        DavItem di = null;
        di = (DavItem)m_davItemCache.get( dp.toString());
        
        if( di == null )
        {
            di = getItemNoCache( dp );
            m_davItemCache.put( dp.toString(), di );
            cache.put("m_davItemCache", m_davItemCache);
        }

        return di;
    }

    protected DavItem getItemNoCache( DavPath path )
    {
        String pname = path.filePart();
        
        //
        //  Lists top-level elements
        //
        if( path.isRoot() )
        {
            log.log(Level.INFO,"Adding DAV items from path "+path);
            DirectoryItem di = new DirectoryItem( this, path );
            
            di.addDavItems( listAlphabeticals(path) );
            
            return di;
        }

        //
        //  Lists each item in each subdir
        //
        if( path.isDirectory() )
        {
            log.log(Level.INFO,"Listing pages in path "+path);
            
            DirectoryItem di = new DirectoryItem( this, path );
            
            di.addDavItems( listDirContents(path) );
            
            return di;
        }
        
        if( pname.endsWith(".txt") && pname.length() > 4 )
        {
            pname = pname.substring(0,pname.length()-4);
        }
        
        WikiPage page = m_engine.getPage( pname );
        
        if( page != null )
        {
            return new PageDavItem( this, path, page );
        }
        
        return null;
    }

    public void setItem( DavPath path, DavItem item )
    {
    // TODO Auto-generated method stub

    }

}
